feat: add tests for guide code snippets#552
Conversation
804f216 to
352ef6f
Compare
There was a problem hiding this comment.
Pull request overview
Adds an integration-test framework to compile/check code blocks in the docs/ guide, and updates multiple guide snippets so they build under the new framework.
Changes:
- Introduces
cot-testcrate +libtest-mimicrunner to validate fenced code blocks indocs/*.md(Rust/TOML/Askama templates). - Updates many guide snippets (hidden
# ...lines, async fixes, etc.) to compile under the new checking approach. - Adds supporting glue:
just test-docs, nextest concurrency override, and small framework/API touch-ups (e.g.,TestRequestBuilder::cache,ForeignKey::new).
Reviewed changes
Copilot reviewed 25 out of 27 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| justfile | Adds test-docs task/alias to run the new doc-snippet tests. |
| docs/upgrade-guide.md | Adjusts a Rust code fence and list indentation. |
| docs/testing.md | Updates examples; adds async fixes; minor formatting changes. |
| docs/templates.md | Updates Askama examples and adds extra supporting code for compilation. |
| docs/static-files.md | Adds hidden lines so static-file snippets compile. |
| docs/site/Cargo.lock | Bumps cot-site git rev in the docs site lockfile. |
| docs/README.md | Reworks headings and adds placeholder section for doc-test instructions. |
| docs/openapi.md | Updates OpenAPI examples (return types, derives, route registration) to compile. |
| docs/introduction.md | Adds hidden scaffolding + a rust,has_main snippet for the main example. |
| docs/forms.md | Updates form examples to use newer APIs (into_response()?) and adds hidden scaffolding. |
| docs/db-models.md | Makes DB examples compile (adds hidden model scaffolding; comments out sample URLs). |
| docs/caching.md | Adds hidden scaffolding so cache examples compile. |
| docs/admin-panel.md | Updates admin-panel snippets for newer APIs and adds missing scaffolding. |
| cot/src/test.rs | Adds TestRequestBuilder::cache() behind cache feature. |
| cot/src/project.rs | Updates a panic message to reference cot::Project. |
| cot/src/db/relations.rs | Adds ForeignKey::new(...) convenience constructor. |
| cot-test/tests/doc_code_blocks.rs | New libtest-mimic runner that parses Markdown and runs per-block checks. |
| cot-test/templates/html_main.rs | Template-compilation harness for Askama snippets. |
| cot-test/templates/base.html | Base template used by Askama snippet compilation. |
| cot-test/src/lib.rs | New helper crate that generates a temp project and runs cargo check for snippets. |
| cot-test/Cargo.toml | Defines new internal crate and custom test harness entry. |
| cot-macros/tests/ui/func_query_invalid_field.stderr | Updates expected UI test stderr output wording. |
| cot-macros/tests/ui/attr_main_args.stderr | Updates expected stderr to reference cot::Project. |
| cot-macros/tests/ui/attr_main_args.rs | Updates return type to cot::Project. |
| Cargo.toml | Adds cot-test to workspace + new workspace deps (comrak, libtest-mimic, etc.). |
| Cargo.lock | Updates lockfile for new deps and cot-test crate. |
| .config/nextest.toml | Adds nextest grouping to serialize doc-code-block tests. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
| Branch | docs-tests-2 |
| Testbed | github-ubuntu-latest |
Click to view all benchmark results
| Benchmark | Latency | Benchmark Result microseconds (µs) (Result Δ%) | Upper Boundary microseconds (µs) (Limit %) |
|---|---|---|---|
| empty_router/empty_router | 📈 view plot 🚷 view threshold | 5,757.40 µs(-5.27%)Baseline: 6,077.48 µs | 7,391.53 µs (77.89%) |
| json_api/json_api | 📈 view plot 🚷 view threshold | 1,027.30 µs(-3.19%)Baseline: 1,061.12 µs | 1,264.88 µs (81.22%) |
| nested_routers/nested_routers | 📈 view plot 🚷 view threshold | 985.79 µs(+1.05%)Baseline: 975.52 µs | 1,153.86 µs (85.43%) |
| single_root_route/single_root_route | 📈 view plot 🚷 view threshold | 961.72 µs(+2.51%)Baseline: 938.20 µs | 1,118.80 µs (85.96%) |
| single_root_route_burst/single_root_route_burst | 📈 view plot 🚷 view threshold | 19,807.00 µs(+12.03%)Baseline: 17,679.47 µs | 21,082.66 µs (93.95%) |
75dc48c to
5b957f4
Compare
This adds a test framework to test the code snippets in the guide (the docs/ directory). Currently, the framework defines four different test configurations: * `toml`, tested by parsing as a Cot config file * `html.j2`, tested by trying to build an Askama template * `rust`, tested by building the snippet as an extension of a default project template (as generated by `cot-cli`) * `rust,has_main`, a version of the above that also defines its own `main` function It should be fairly easy to extend this by defining more configurations, if needed. The framework defines a number of utilities to make the testing more pleasant: * Many symbols are imported by default * Similarly to rustdoc, lines can be prepended with `# ` to indicate lines that are not displayed in the guide, but required for the test to build * For the Askama tests, there are some dummy files and structures created This is part of the major effort to make the guide always up-to-date. As a result of this, a number of code snippets were modified as they did not build before. This concludes the work on #471.
|
Note: this PR still needs a corresponding one in cot-site for hiding rust lines starting with |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 23 out of 25 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Codecov Report❌ Patch coverage is
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
| // For specific symbols, only add if the symbol is NOT already imported | ||
| let mut found = false; | ||
| for line in code.lines() { | ||
| if line.starts_with("use ") && line.contains(symbol_name) { |
There was a problem hiding this comment.
Will this handle cases where symbol names are aliased and not necessarily what we're looking for?
Eg, use cot::common_types::Email and use cot::email::Email as EmailService. If we have common_types::Email in COMMON_IMPORTS, then use cot::email::Email as EmailService could still be a match. No?
| } | ||
|
|
||
| let final_code = match test_type { | ||
| RUST_HAS_MAIN_TEST_TYPE => code.to_string(), |
There was a problem hiding this comment.
Shouldn't we use an enum for this in case we add more variants in the future?
| self | ||
| } | ||
|
|
||
| /// Add a cache to the request builder. |
There was a problem hiding this comment.
wondering if we'll also need to add one for email
| test_runners.insert(("rust", DEFAULT_TEST_NAME), test_rust); | ||
| test_runners.insert(("rust", RUST_HAS_MAIN_TEST_TYPE), test_rust); | ||
| test_runners.insert(("toml", DEFAULT_TEST_NAME), test_toml); | ||
| test_runners.insert(("html.j2", DEFAULT_TEST_NAME), test_html); |
There was a problem hiding this comment.
I think I'd like an enum for language options too
This adds a test framework to test the code snippets in the guide (the docs/ directory). Currently, the framework defines four different test configurations:
toml, tested by parsing as a Cot config filehtml.j2, tested by trying to build an Askama templaterust, tested by building the snippet as an extension of a default project template (as generated bycot-cli)rust,has_main, a version of the above that also defines its ownmainfunctionIt should be fairly easy to extend this by defining more configurations, if needed.
The framework defines a number of utilities to make the testing more pleasant:
#to indicate lines that are not displayed in the guide, but required for the test to buildThis is part of the major effort to make the guide always up-to-date. As a result of this, a number of code snippets were modified as they did not build before.
This concludes the work on #471.
Type of change